var prefix = jsctx + "/parkingOrderInfo";

var parkingOrderInfo = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;
    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageParkingOrder",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [10,20,30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            orderNum:$("#orderNum").val().trim(),
                            orderStartTime:$("#orderStartTime").val().trim(),
                            orderEndTime:$("#orderEndTime").val().trim(),
                            outTradeNum:$("#outTradeNum").val().trim(),
                            carNumber:$("#carNumber").val().trim(),
                            stationName:$("#stationName").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width:'50',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'orderNum', title: '订单号', align: 'center'},
                        {field: 'stationName', title: '桩群名', align: 'center'},
                        {field: 'stationNo', title: '桩群编号', align: 'center'},
                        {
                            field: 'status', title: '订单状态', align: 'center',
                            formatter: function (value, row, index) {
                                if (row.orderType == '0') {
                                    if (row.outOrderStatus == '0'){
                                        return '未处理';
                                    }else if(row.outOrderStatus == '1'){
                                        return '已处理';
                                    }else if (row.outOrderStatus == '2'){
                                        return '额外优惠';
                                    }

                                } else if (row.orderType == '1') {
                                    if (row.selfOrderStatus == '0'){
                                        return "进场未支付";
                                    }else if(row.selfOrderStatus == '1'){
                                        return "出场未支付";
                                    }else if(row.selfOrderStatus == '2'){
                                        return "出场已支付";
                                    }else if(row.selfOrderStatus == '3'){
                                        return "已出场";
                                    }
                                }else {
                                    return "未知";
                                }
                            }

                        },
                        {
                            field: 'tempParkStatus', title: '临时停车状态', align: 'center',
                            formatter: function (value, row, index) {
                                if (row.tempParkStatus == '4'){
                                    return "出闸";
                                }else if (row.tempParkStatus == '3'){
                                    return "出闸扫码";
                                }else if (row.tempParkStatus == '2'){
                                    return "入闸";
                                }else if (row.tempParkStatus == '1'){
                                    return "入闸扫码";
                                }else {
                                    return "非临时车";
                                }
                            }
                        },
                        {field: 'orderStartTime', title: '开始时间', align: 'center'},
                        {field: 'orderEndTime', title: '结束时间', align: 'center'},
                        {field: 'entryTime', title: '入场时间', align: 'center'},
                        {field: 'outTime', title: '出场时间', align: 'center'},
                        {field: 'carNumber', title: '车牌号', align: 'center'},
                        {field: 'chargeDuration', title: '充电时长', align: 'center'},
                        {field: 'outTradeNum', title: '停车订单号', align: 'center'},
                        {field: 'outTicketNum', title: '优惠券单号', align: 'center'},
                        {field: 'remark', title: '备注', align: 'center'},
                        {field: 'createdTime', title: '创建时间', align: 'center'},
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" onclick="parkingOrderInfo.unbind_TJD(\''+
                                        row.outTradeNum +'\',\''+ row.outTicketNum + '\',' + row.parkingOrderId + ',' + row.parkingSupplierId +')">解绑</a> ';
                                if (row.stationNo == 'JT0124' || row.stationNo == 'JT0140'){
                                    if (row.outOrderStatus != '2'){
                                        return a;
                                    }
                                }
                            }
                        }
                    ]
                });
    }
    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        window.history.back();
    }

    function unbind_TJD(outTradeNum, outTicketNum, parkingOrderId, parkingSupplierId) {
        $.ajax({
            cache: false,
            type: "POST",
            url: prefix + "/unbind_TJD",
            data:{
                outTradeNum:outTradeNum,
                outTicketNum:outTicketNum,
                parkingOrderId:parkingOrderId,
                parkingSupplierId:parkingSupplierId
            },
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    reLoad();
                } else {
                    console.warn(data.message);
                    layer.msg(data.message);
                }
            }
        });
    }

    function exportParkingOrder() {
        $.fileDownload(prefix + "/exportParkingOrder", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text(),
                count:$("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                orderNum:$("#orderNum").val().trim(),
                orderStartTime:$("#orderStartTime").val().trim(),
                orderEndTime:$("#orderEndTime").val().trim(),
                outTradeNum:$("#outTradeNum").val().trim(),
                carNumber:$("#carNumber").val().trim(),
                stationName:$("#stationName").val().trim(),
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...",{time:-1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！",{time:1500});
            },
            failCallback: function (data) {
                var str = "{"+data.substring(data.indexOf("{")+1,data.indexOf("}"))+"}";
                layer.msg(JSON.parse(str).message);
            }
        });
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        unbind_TJD:unbind_TJD,
        exportParkingOrder:exportParkingOrder
    };
})();

